home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter7 / crdrag.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  16KB  |  462 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "crdrag.h"  
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "App";
  20. LPCTSTR lpszTitle   = "TreeView_CreateDragImage()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106. VOID AddItemsToTree( HWND hTree );
  107.  
  108. #define IMAGESIZE 16
  109.  
  110. LPCTSTR lpszData[4] = { "Circle", "Rectangle", "Cross", "Check" };
  111.  
  112. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  113. {
  114. static TV_HITTESTINFO tvHitTest;
  115. static POINT          ptHotSpot;
  116. static HTREEITEM      hDragItem = NULL;
  117. static HWND           hTree     = NULL;
  118. static HIMAGELIST     hCursors  = NULL;
  119. static HIMAGELIST     hDragList = NULL;
  120.  
  121.    switch( uMsg )
  122.    {
  123.       case WM_CREATE  :
  124.               {
  125.                  HIMAGELIST hImage;
  126.                  ICONINFO   info;
  127.  
  128.                  InitCommonControls();
  129.  
  130.                  hCursors = ImageList_Create( 32, 32, ILC_COLOR4 | ILC_MASK, 1, 1 ); 
  131.                  hImage   = ImageList_Create( IMAGESIZE, IMAGESIZE, 
  132.                                               ILC_COLOR4 | ILC_MASK, 4, 1 ); 
  133.                  hTree = CreateWindowEx( WS_EX_CLIENTEDGE, WC_TREEVIEW, "",
  134.                                          WS_CHILD | WS_BORDER | WS_VISIBLE | 
  135.                                          WS_VSCROLL | TVS_HASLINES | 
  136.                                          TVS_HASBUTTONS | TVS_LINESATROOT,
  137.                                          10, 100, 100, 100,
  138.                                          hWnd,
  139.                                          (HMENU)1,
  140.                                          hInst,
  141.                                          NULL);
  142.  
  143.                  if ( hTree )
  144.                  {
  145.                     int i;
  146.  
  147.                     // Associate the image list with the tree view.
  148.                     //.............................................
  149.                     TreeView_SetImageList( hTree, hImage, TVSIL_NORMAL );
  150.  
  151.                     // Add the images to the tree view image list.
  152.                     //............................................
  153.                     for ( i=0; i<4; i++ )
  154.                        ImageList_AddIcon( hImage, LoadIcon( hInst, lpszData[i] ) );
  155.  
  156.                     AddItemsToTree( hTree );
  157.                  }
  158.  
  159.                  // Create drag cursor icon.
  160.                  //.........................
  161.                  ImageList_AddIcon( hCursors, LoadCursor( NULL, IDC_ARROW ) );
  162.  
  163.                  // Retrieve the hot spot information from the cursor.
  164.                  //...................................................
  165.                  GetIconInfo( LoadCursor( NULL, IDC_ARROW ), &info );
  166.                  ptHotSpot.x = info.xHotspot;
  167.                  ptHotSpot.y = info.yHotspot;
  168.  
  169.                  // Clean up the bitmaps from GetIconInfo().
  170.                  //.........................................
  171.                  DeleteObject( info.hbmMask );
  172.                  DeleteObject( info.hbmColor );
  173.               }
  174.               break;
  175.  
  176.       case WM_SIZE :
  177.               if ( hTree )
  178.                  MoveWindow( hTree, 0, 0, LOWORD( lParam ), HIWORD( lParam ), FALSE );
  179.               break;
  180.  
  181.  
  182.       case WM_NOTIFY :
  183.               if ( wParam == 1 )
  184.               {
  185.                  NM_TREEVIEW* pNM = (NM_TREEVIEW*)lParam;
  186.  
  187.                  switch ( pNM->hdr.code )
  188.                  {
  189.                     case TVN_BEGINDRAG :                 
  190.                            {
  191.                               RECT rc;
  192.  
  193.                               // Create the drag image.
  194.                               //.......................
  195.                               hDragList = TreeView_CreateDragImage( hTree, pNM->itemNew.hItem );
  196.  
  197.                               TreeView_GetItemRect( hTree, pNM->itemNew.hItem, &rc, FALSE );
  198.  
  199.                               // Start dragging and set the drag cursor.
  200.                               //........................................
  201.                               ImageList_BeginDrag( hDragList, 0, 
  202.                                             pNM->ptDrag.x-rc.left-IMAGESIZE, 
  203.                                             pNM->ptDrag.y-rc.top );
  204.                               ImageList_SetDragCursorImage( hCursors, 0, 
  205.                                             pNM->ptDrag.x-rc.left-ptHotSpot.x-IMAGESIZE, 
  206.                                             pNM->ptDrag.y-rc.top-ptHotSpot.y );
  207.                               ImageList_DragEnter( hTree, pNM->ptDrag.x, pNM->ptDrag.y ); 
  208.  
  209.                               // Capture the mouse and hide the normal cursor.
  210.                               //..............................................
  211.                               SetCapture( hWnd );
  212.                               ShowCursor( FALSE );
  213.  
  214.                               hDragItem = pNM->itemNew.hItem;
  215.                            }
  216.                            break;
  217.  
  218.                  } 
  219.               }
  220.               break;
  221.  
  222.       case WM_MOUSEMOVE :
  223.               // Move the drag cursor if dragging.
  224.               //..................................
  225.               if ( hDragList )
  226.               {
  227.                  tvHitTest.pt.x = LOWORD( lParam );
  228.                  tvHitTest.pt.y = HIWORD( lParam );
  229.  
  230.                  // Check to see if the item is over a tree view item,
  231.                  // if it is, then it is hilighted.
  232.                  //...................................................
  233.                  TreeView_HitTest( hTree, &tvHitTest );
  234.                  if ( tvHitTest.hItem != TreeView_GetDropHilight( hTree ) )
  235.                  {
  236.                     // Clear the drag image.
  237.                     //......................
  238.                     ImageList_DragLeave( hTree );
  239.  
  240.                     // Unhilight the old item, hilight the new one.
  241.                     //.............................................
  242.                     TreeView_SelectDropTarget( hTree, NULL );
  243.                     TreeView_SelectDropTarget( hTree, tvHitTest.hItem );
  244.  
  245.                     // Show the drag image.
  246.                     //.....................
  247.                     ImageList_DragEnter( hTree, LOWORD( lParam ), HIWORD( lParam ) ); 
  248.                  }
  249.                  else   
  250.                     ImageList_DragMove( LOWORD( lParam ), HIWORD( lParam ) );
  251.               }
  252.               break;
  253.  
  254.  
  255.       case WM_LBUTTONUP :
  256.               // If dragging, drop the object in its new place.
  257.               //...............................................
  258.               if ( hDragList )
  259.               {
  260.                  HTREEITEM hDropItem;
  261.  
  262.                  // Stop the dragging process.
  263.                  //...........................
  264.                  ImageList_DragLeave( hTree );
  265.                  ImageList_EndDrag();
  266.                  ImageList_Destroy( hDragList );
  267.                  hDragList = NULL;
  268.                  ReleaseCapture();
  269.                  ShowCursor( TRUE );
  270.  
  271.                  // Find out the item that was last hilighted.
  272.                  //...........................................
  273.                  hDropItem = TreeView_GetDropHilight( hTree );
  274.  
  275.                  // Unselect the drop hilight item.
  276.                  //................................
  277.                  TreeView_SelectDropTarget( hTree, NULL );
  278.  
  279.                  // Item was dropped, move the item in the tree.
  280.                  //.............................................
  281.                  if ( hDropItem )
  282.                  {
  283.                     TV_INSERTSTRUCT tv;
  284.                     TV_ITEM         tvDropItem;
  285.                     char            szName[40];
  286.                     
  287.                     // Retrieve the Drag Item.
  288.                     //........................  
  289.                     tv.item.hItem      = hDragItem;
  290.                     tv.item.mask       = TVIF_TEXT | TVIF_IMAGE | 
  291.                                          TVIF_SELECTEDIMAGE | TVIF_PARAM;
  292.                     tv.item.pszText    = szName;
  293.                     tv.item.cchTextMax = sizeof( szName );
  294.                     TreeView_GetItem( hTree, &tv.item );
  295.  
  296.                     // Retrieve the Drop Item.
  297.                     //........................
  298.                     tvDropItem.hItem      = hDropItem;
  299.                     tvDropItem.mask       = TVIF_PARAM;
  300.                     TreeView_GetItem( hTree, &tvDropItem );
  301.  
  302.                     // Only let level 1 items go to level 1 and
  303.                     // level 2 items go to level 2.
  304.                     //.........................................
  305.                     if ( tv.item.lParam && tvDropItem.lParam )
  306.                     {
  307.                        tv.hParent = TreeView_GetParent( hTree, hDropItem );
  308.                        tv.hInsertAfter = hDropItem;
  309.                     }
  310.                     else if ( tv.item.lParam && !tvDropItem.lParam )
  311.                     {
  312.                        tv.hParent = hDropItem;
  313.                        tv.hInsertAfter = TVI_LAST;
  314.                     }
  315.                     else if ( !tv.item.lParam && tvDropItem.lParam )
  316.                     {
  317.                        tv.hParent = TVI_ROOT;
  318.                        tv.hInsertAfter = TreeView_GetParent( hTree, hDropItem );
  319.                     }
  320.                     else if ( !tv.item.lParam && !tvDropItem.lParam )
  321.                     {
  322.                        tv.hParent = TVI_ROOT;
  323.                        tv.hInsertAfter = hDropItem;
  324.                     }
  325.  
  326.                     // Insert the dragged item into the tree
  327.                     // in the new location.
  328.                     //......................................
  329.                     tv.hParent = TreeView_InsertItem( hTree, &tv );
  330.  
  331.                     // Move the children if there are any.
  332.                     //....................................
  333.                     if ( !tv.item.lParam )
  334.                     {
  335.                        tv.hInsertAfter = TVI_LAST;
  336.                        tv.item.hItem = TreeView_GetChild( hTree, hDragItem );
  337.                        while ( tv.item.hItem )
  338.                        {
  339.                           TreeView_GetItem( hTree, &tv.item );
  340.                           TreeView_InsertItem( hTree, &tv );
  341.                           tv.item.hItem = TreeView_GetNextSibling( hTree, tv.item.hItem );
  342.                        }
  343.                     }
  344.  
  345.                     // Delete the original item from the tree,
  346.                     // this is because we are moving the item.
  347.                     //........................................
  348.                     TreeView_DeleteItem( hTree, hDragItem ); 
  349.                  }
  350.  
  351.                  // Update the tree view.
  352.                  //......................
  353.                  InvalidateRect( hTree, NULL, TRUE );
  354.  
  355.               }
  356.               break;
  357.  
  358.  
  359.       case WM_COMMAND :
  360.               switch( LOWORD( wParam ) )
  361.               {
  362.                  case IDM_RESET :
  363.                         // Clear the tree view.
  364.                         //.....................
  365.                         TreeView_DeleteAllItems( hTree );
  366.  
  367.                         // Add the items to the tree.
  368.                         //...........................
  369.                         AddItemsToTree( hTree );
  370.                         break;
  371.  
  372.                  case IDM_ABOUT :
  373.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  374.                         break;
  375.  
  376.                  case IDM_EXIT :
  377.                         DestroyWindow( hWnd );
  378.                         break;
  379.               }
  380.               break;
  381.       
  382.       case WM_DESTROY :
  383.               if ( TreeView_GetImageList( hTree, TVSIL_NORMAL ) )
  384.                  ImageList_Destroy( TreeView_GetImageList( hTree, TVSIL_NORMAL ) );
  385.  
  386.               PostQuitMessage(0);
  387.               break;
  388.  
  389.       default :
  390.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  391.    }
  392.  
  393.    return( 0L );
  394. }
  395.  
  396.  
  397. VOID AddItemsToTree( HWND hTree )
  398. {
  399.    int             i, j;
  400.    char            szTmp[40];
  401.    TV_INSERTSTRUCT tv;
  402.    HTREEITEM       hParent;
  403.  
  404.    tv.hInsertAfter = TVI_LAST;
  405.    tv.item.mask    = TVIF_TEXT | TVIF_IMAGE | 
  406.                      TVIF_SELECTEDIMAGE | TVIF_PARAM;
  407.  
  408.    // Add the items to the tree view.
  409.    //................................
  410.    for ( i=0; i<20; i++ )
  411.    {
  412.       wsprintf( szTmp, "%s %d", lpszData[0], i+1 );
  413.  
  414.       tv.hParent      = TVI_ROOT;
  415.       tv.item.pszText = szTmp;
  416.       tv.item.iImage  = 0;
  417.       tv.item.iSelectedImage = 0;
  418.       tv.item.lParam         = 0;
  419.  
  420.       hParent = TreeView_InsertItem( hTree, &tv );
  421.       tv.hParent = hParent;
  422.  
  423.       // Add child items.
  424.       //.................
  425.       for( j=1; j<4; j++ )
  426.       {
  427.          tv.item.pszText = (LPTSTR)lpszData[j];
  428.          tv.item.iImage  = j;
  429.          tv.item.iSelectedImage = j;
  430.          tv.item.lParam         = 1;
  431.  
  432.          TreeView_InsertItem( hTree, &tv );
  433.       }
  434.    }
  435. }
  436.  
  437.  
  438. LRESULT CALLBACK About( HWND hDlg,           
  439.                         UINT message,        
  440.                         WPARAM wParam,       
  441.                         LPARAM lParam)
  442. {
  443.    switch (message) 
  444.    {
  445.        case WM_INITDIALOG: 
  446.                return (TRUE);
  447.  
  448.        case WM_COMMAND:                              
  449.                if (   LOWORD(wParam) == IDOK         
  450.                    || LOWORD(wParam) == IDCANCEL)    
  451.                {
  452.                        EndDialog(hDlg, TRUE);        
  453.                        return (TRUE);
  454.                }
  455.                break;
  456.    }
  457.  
  458.    return (FALSE); 
  459. }
  460.  
  461.  
  462.